<template>
  <el-container>
    <el-header>
      <div>
        <img src="../assets/heima.png" alt />
        <span>商城后台管理系统</span>
      </div>
      <el-button type="info" plain @click="loginOut">退出登录</el-button>
    </el-header>
    <el-container>
      <el-aside :width="isCollapse ? '64px' : '200px' ">
        <div @click="collapseMenu">亖</div>
        <el-menu
          background-color="#545c64"
          text-color="#fff"
          active-text-color="#6463ea"
          unique-opened
          router
          :collapse="isCollapse"
          :collapse-transition="false"
          :default-active="activeMenu"
        >
          <el-submenu v-for="subitems in menus" :key="subitems.id" :index="subitems.id+''">
            <template slot="title">
              <i :class="iconsObj[subitems.id]"></i>
              <span>{{subitems.authName}}</span>
            </template>
            <el-menu-item
              :index="'/'+items.path"
              v-for="items in subitems.children"
              :key="items.id"
              @click="setActiveMenu('/'+items.path)"
            >
              <template slot="title">
                <i class="el-icon-s-grid"></i>
                <span>{{items.authName}}</span>
              </template>
            </el-menu-item>
          </el-submenu>
        </el-menu>
      </el-aside>
      <el-main>
        <router-view></router-view>
      </el-main>
    </el-container>
  </el-container>
</template>

<script>
export default {
  data () {
    return {
      menus: [],
      iconsObj: {
        125: 'el-icon-user',
        103: 'el-icon-data-line',
        101: 'el-icon-shopping-bag-2',
        102: 'el-icon-s-order',
        145: 'el-icon-pie-chart'
      },
      isCollapse: false,
      activeMenu: ''
    }
  },
  created () {
    this.getMenus()
  },
  methods: {
    loginOut () {
      window.sessionStorage.clear()
      this.$router.push('/login')
    },
    async getMenus () {
      const { data: res } = await this.$http.get('menus')
      this.menus = res.data
      this.activeMenu = window.sessionStorage.getItem('activeMenu')
    },
    collapseMenu () {
      this.isCollapse = !this.isCollapse
    },
    setActiveMenu (activeMenu) {
      window.sessionStorage.setItem('activeMenu', activeMenu)
      this.activeMenu = activeMenu
    }
  }
}
</script>

<style lang="less" scoped>
.el-container {
  height: 100%;
  .el-header {
    display: flex;
    background-color: rgb(72, 117, 221);
    padding: 0 10px;
    color: #fff;
    align-items: center;
    font-size: 20px;
    justify-content: space-between;

    div {
      display: flex;
      align-items: center;
      span {
        margin-left: 10px;
      }
    }
  }
  .el-aside {
    background-color: rgb(110, 143, 219);
    > div {
      text-align: center;
      color: #fff;
      cursor: pointer;
      background-color: rgb(67, 74, 80);
      line-height: 30px;
    }
  }
  .el-menu {
    border-right: none;
  }
}
</style>
